package com.glowworms.core.web.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 上下文持有者
 */
public class ContextHolder implements ServletContextListener {

	/**
	 * Spring上下文
	 */
	private static ApplicationContext applicationContext;
	
	/**
	 * 应用上下文
	 */
	private static ServletContext servletContext;
	
	/**
	 * 
	 */
	public void contextDestroyed(ServletContextEvent arg0) {
		applicationContext = null;
		servletContext = null;
	}

	/**
	 * 
	 */
	public void contextInitialized(ServletContextEvent arg0) {
		servletContext = arg0.getServletContext();
		applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
	}
	
	/**
	 * 
	 * @return
	 */
	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	/**
	 * 
	 * @return
	 */
	public static ServletContext getServletContext() {
		return servletContext;
	}
}
